//
//  WBEmoticonTabberTool.swift
//  weibo
//
//  Created by  郭航 on 16/7/4.
//  Copyright © 2016年 郭航. All rights reserved.
//

import UIKit

enum WBEmoticonTabberToolType: Int {
    case left = 1
    case mid = 2
    case right = 3
}

private let titles = ["默认", "Emoji", "浪小花"]

private let images = [
            "compose_emotion_table_left_normal",
            "compose_emotion_table_mid_normal",
            "compose_emotion_table_right_normal"
                        ]
private let imageSelected = [
            "compose_emotion_table_left_selected",
            "compose_emotion_table_mid_selected",
            "compose_emotion_table_right_selected"
                        ]

class WBEmoticonTabberTool: UIStackView {
    
    var callBack: ((type: WBEmoticonTabberToolType) -> ())?

    private var buttons = [UIButton]()
    
    private var selectedButton: UIButton? {
        didSet {
            if selectedButton == oldValue {
                return
            }
            
            oldValue?.selected = false
            selectedButton?.selected = true
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.whiteColor()
        
        axis = .Horizontal
        distribution = .FillEqually
        
        buttons = addButtons(titles, images: images, imageSelected: imageSelected)
        
        selectedButton = buttons[0]
        selectedButton?.selected = true
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func addButtons(titles: [String], images: [String], imageSelected: [String]) -> [UIButton] {
        var buttons = [UIButton]()
        
        for i in 0 ..< titles.count {
            let button = UIButton()
            
            button.addTarget(self, action: #selector(WBEmoticonTabberTool.buttonClick(_:)), forControlEvents: .TouchUpInside)
            
            button.tag = i + 1
            button.setTitle(titles[i], forState: .Normal)
            button.setBackgroundImage(UIImage(named: images[i]), forState: .Normal)
            button.setBackgroundImage(UIImage(named: imageSelected[i]), forState: .Selected)
            button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
            button.setTitleColor(UIColor.grayColor(), forState: .Selected)
            button.titleLabel?.font = UIFont.systemFontOfSize(15)
            
            addArrangedSubview(button)
            
            buttons.append(button)
        }
        
        return buttons
    }

    @objc private func buttonClick(button: UIButton) -> Void {
        if selectedButton == button {
            return
        }
        
        selectedButton = button
        
        callBack?(type: WBEmoticonTabberToolType(rawValue: button.tag)!)
    }
    
    func setSelectButton(type: WBEmoticonTabberToolType) -> Void {
        //selectedButton = viewWithTag(type.rawValue) as? UIButton
        
        selectedButton = buttons[type.rawValue - 1]
    }
    
}
